package edu.userclient.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import edu.userclient.entity.User;
import edu.userclient.repository.UserRepository;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private EurekaClient eurekaClient;

	@Autowired
	private DiscoveryClient discoveryClient;

	@Value("${server.port}")
	private Integer requestPort;

	@GetMapping(value = "/user/{id}")
	public User findUserById(@PathVariable(value = "id") long id){
		LoggerFactory.getLogger(this.getClass()).info("UserClient_1提供服务,端口号：8881");
		User user = userRepository.findOne(id);
		user.setRequestPort(requestPort);
		return user;
	}

	@GetMapping(value = "/serverUrl-eureka")
	public String seuverUrl_eureka(){
		InstanceInfo info = this.eurekaClient.getNextServerFromEureka("UserService",false);
		return info.getHomePageUrl();
	}

	@GetMapping(value = "/serverUrl-discovery")
	public ServiceInstance seuverUrl_discovery(){
		ServiceInstance serviceInstance = this.discoveryClient.getLocalServiceInstance();
		return serviceInstance;
	}

	@Value("${profile}")
	private String profile;
	@GetMapping(value = "/profile")
	public String getProfile(){
		return profile;
	}

}
